package echo.codec;

import echo.protocol.MessageProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class MessageDecoder extends ByteToMessageDecoder {
    private static final AtomicInteger sum = new AtomicInteger();
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        int id = in.readInt();
        int len = in.readInt();
        byte[] content = new byte[0];
        in.readBytes(content);
        MessageProtocol messageProtocol = new MessageProtocol(id, len, content);
        out.add(messageProtocol);
    }
}
